package com.projectName.www.servlet;
import com.projectName.www.po.User;
import com.projectName.www.service.UserService;
import com.projectName.www.service.UserServiceImpl;
import com.projectName.www.util.Beanutils;
import com.projectName.www.util.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/addUserServlet")
public class AddUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //0.设置字符集
        request.setCharacterEncoding("utf-8");
        //1.获取id
        Map<String,String[]> map=request.getParameterMap();

        //3.封装对象
//        User user=new User();
        User user=new User();

        System.out.println();
        try {
            //把user的值传进map
            Beanutils.populate(user,map);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        //4.调用一个service保存
        UserService us = new UserServiceImpl();
        if (user.getUserType().equals(2)){
            //r如果是商家需要审核通过才可以
            user.setOnlinestatus(0);
        }else{
            user.setOnlinestatus(1);
        }
        us.insert(user);


        //5、跳转到userListServlet，使用重定向
        response.sendRedirect(request.getContextPath()+"/userListServlet");



    }
        //2.获取参数
//        String userName = request.getParameter("userName");
//        String password = MD5Util.string2MD5(request.getParameter("password"));
//
//        String address = request.getParameter("address");
//        String contact = request.getParameter("contact");
//        String userType = request.getParameter("userType");
//        String info = request.getParameter("info");
//
//        //3.封装对象
//        User user = new User();
//        user.setUserName(userName);
//        user.setPassword(password);
//        user.setUserType(Integer.parseInt(userType));
//        user.setContact(contact);
//        user.setAddress(address);
//        user.setJianjie(info);


//        //4.调用Service保存
//        UserService service = new UserServiceImpl();
//        service.insert(user);
//
//        //5.跳转到userListServlet
//        response.sendRedirect(request.getContextPath()+"/userListServlet");


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
